import { excelToJson } from "./config"
var file = null

// 将导入的excel转为 json
function importfxx(obj) {
    return new Promise((res, rej) => {
        let _this = this;
        // 通过DOM取文件数据
        file = obj
        var rABS = false; //是否将文件读取为二进制字符串
        var f = file;
        var reader = new FileReader();
        //if (!FileReader.prototype.readAsBinaryString) {
        FileReader.prototype.readAsBinaryString = function (f) {
            var binary = "";
            var rABS = false; //是否将文件读取为二进制字符串
            var pt = this;
            var wb; //读取完成的数据
            var outdata;
            var reader = new FileReader();
            reader.onload = function (e) {
                var bytes = new Uint8Array(reader.result);
                var length = bytes.byteLength;
                for (var i = 0; i < length; i++) {
                    binary += String.fromCharCode(bytes[i]);
                }
                var XLSX = require('xlsx');
                if (rABS) {
                    wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
                        type: 'base64'
                    });
                } else {
                    wb = XLSX.read(binary, {
                        type: 'binary'
                    });
                }
                outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);//outdata就是你想要的东西
                let arr = []
                let length1 = outdata.length
                let length2 = excelToJson.length
                for (let i = 0; i < length1; i++) {
                    let obj = {}
                    for (let item in outdata[i]) {
                        for (let j = 0; j < length2; j++) {
                            if (excelToJson[j].name == item) {
                                // console.log(excelToJson[j].type, outdata[i][item])
                                obj[excelToJson[j].type] = outdata[i][item]
                            }
                        }
                    }

                    arr.push(obj)
                }

                // outdata.map(v => {
                //     let obj = {}
                //     obj.id = v['id']
                //     obj.username = v['姓名']
                //     obj.sex = v['性别']
                //     obj.age = v['年龄']
                //     arr.push(obj)
                // })
                res(arr)
            }
            reader.readAsArrayBuffer(f);
        }

        if (rABS) {
            reader.readAsArrayBuffer(f);
        } else {
            reader.readAsBinaryString(f);
        }
    })
}

function getExcel(res) {
    require.ensure([], () => {
        const { export_json_to_excel } = require('@/tool/Export2Excel.js')
        // const tHeader = ['id', '姓名', '年龄', '性别']
        const tHeader=excelToJson.map(item=>{
            return item.name
        })
        const filterVal=excelToJson.map(item=>{
            return item.type
        })
        // const filterVal = ['id', 'name', 'age', 'sex']
        const list = res
        const data = formatJson(filterVal, list)
        export_json_to_excel(tHeader, data, '导出列表名称')
    })
}

function formatJson(filterVal, jsonData) {
    return jsonData.map(v => filterVal.map(j => v[j]))
}

export {
    importfxx,
    getExcel
}